package controllers;

import java.io.IOException;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;

import objects.Constituent;
import objects.Lexicon;

import org.xml.sax.SAXException;

import views.ViewFeatures;
import controllers.listeners.feature.*;
import models.FeaturesModel;
import models.LexiconFeaturesModel;

/* Initializes Features UI and listeners */
public class FeaturesModuleController {
	
	public ViewFeatures view;
	
	public FeaturesModuleController() {
		view = null;
	}
	
	public void initializeView() throws ParserConfigurationException, SAXException, IOException {
		/* Get data from model */
		ArrayList<Constituent> constituents = FeaturesModel.getFeatures(); 
		ArrayList<Lexicon> lexicons = LexiconFeaturesModel.getLexiconFeatures();
		
		/* Initialize view */
		view = new ViewFeatures();
		
		/** Initialize action listeners **/
		
		SaveFeaturesActionListener saveListener = new SaveFeaturesActionListener();
		FeatureNavigationByLabelActionListener navigationListener = new FeatureNavigationByLabelActionListener();
		AddFeatureActionListener addFeatureListener = new AddFeatureActionListener();
		AddFeatureValueActionListener addValueListener = new AddFeatureValueActionListener();
		FeatureNavigationByTypeItemListener comboChooserListener = new FeatureNavigationByTypeItemListener(constituents, lexicons);
		
		navigationListener.setView(view);
		addFeatureListener.setView(view);
		addValueListener.setView(view);
		comboChooserListener.setView(view);
		saveListener.setView(view);
		
		navigationListener.setConstituents(constituents);
		navigationListener.setLexicons(lexicons);
		
		/* Initialize buttons */
		view.addSaveListener(saveListener);
		view.addNavListener(navigationListener);
		view.addAddFeatureListener(addFeatureListener);
		view.addAddValueListener(addValueListener);		
		
		/* Initialize mouse listener */
		FeatureModuleMouseListener tableListener = new FeatureModuleMouseListener();
		tableListener.setView(view);
		view.addTableMouseListener(tableListener);
		
		/* Initialize item listener */
		view.addComboBoxItemListener(comboChooserListener);

		/* Add data to combo-boxes and tables */
		if (constituents != null && constituents.size() > 0) {
			view.setConstituentsForChooser(constituents);
			view.loadConstituent(constituents.get(0));
		}
		/* Make view visible */
		view.setVisible(true);
	}
	
}
